﻿<?php

class Uuch_Reader_YhdReader extends Uuch_Reader_ProductReader{
    
    public function getInfo($url) {
        /*$client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();        
        $body = $response->getBody();*/
        $body = file_get_contents($url);
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name     #id="productMainName">([\W\w.]+?)<#i        
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = strip_tags($matches[0]);
        }
        
        //price           #"nonMemberPrice" price="([\W\w.]+?)">#i        
        /*if(preg_match($config->PriceRegex, $body,$matches)){
            $product->Price = $matches[1];
        }*/
                
        
        //img                 #<img id="productImg"   src="(.+?)" alt#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
                
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }

        if(!is_numeric($id)){
            if(preg_match('/<input.*?id="productMercantId".*?\/>/', $body,$matches)){
                if(preg_match('/value="\d*?"/', $matches[0],$matches)){
                    $matches    = explode("=", $matches[0]);
                    $matches    = str_replace('"', "", $matches[1]);
                    $id         = $matches;
                }
            }
        }
        
        $html = file_get_contents('http://busystock.i.yihaodian.com/restful/detail?mcsite=1&provinceId=20&pmId='. $id);
        $prires = json_decode($html); 
        if ($prires) {
            if(false && $prires->currentTime > time()){
                //这个不会成立
                $product->Price = $prires->promPrice;
            }else{
                // $product->Price = $prires->yhdPrice;
                $product->Price = $prires->currentPrice;
            }
        }
        if(!$product->Price){
            if(preg_match('/价格.*?1号店，品质保证，网上优选/', $body,$matches)){
                $matches    = explode(" ", $matches[0]);
                $matches    = str_replace('￥', "", $matches[1]);
                if(is_numeric($matches)){
                    $product->Price = $matches;
                }
            }
        }
        if(!$product->Price){
            //这里应该无效，应为价格数据不存在页面中
            if(preg_match($config->PriceRegex, $body,$matches)){
                if(count($matches) > 1){
                    $product->Price = $matches[1];
                }
            }
        }
        
        
        if($product->Price >= 100){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        
        $product->Sku = $id;
        $product->Url = $url;
        
        $product->Quantity = 999;
        $shopName = '一号店';
        
        $shop = Application_Model_Shop::getByName($shopName);
        if (NULL == $shop){
            $shop = new Application_Model_Shop();
            $shop->Name = $shopName;
            $shop->IsTaobao = True;
            $shop->Url = $uri['host'];
            $shop->save();
        }
        $product->ShopId = $shop->Id;
        $product->save();
        return $product;
    }
    
    public function getInfoForNew($url) {
        /*$client = new Zend_Http_Client();
        $client->setEncType('');
        $client->setUri($url);
        $response = $client->request();        
        $body = $response->getBody();*/
        $body = file_get_contents($url);
        $uri = parse_url($url);
        $name = explode('.',$uri['host']);
        $reader = ucfirst(strtolower($name[count($name)-2]));
        $config = Application_Model_ReaderConfig::getBySite($reader);
        
        //Name     #id="productMainName">([\W\w.]+?)<#i        
        $product = new Application_Model_Product();
        $matches = array();
        if(preg_match($config->ProductNameRegex, $body,$matches)){
            $product->Name = strip_tags($matches[0]);
        }
        
        //price           #"nonMemberPrice" price="([\W\w.]+?)">#i        
        /*if(preg_match($config->PriceRegex, $body,$matches)){
            $product->Price = $matches[1];
        }*/
                
        
        //img                 #<img id="productImg"   src="(.+?)" alt#im
        
        if(preg_match($config->ImageRegex, $body,$matches)){
            if(preg_match('/http.+?"/', $matches[0],$matches)){
                $product->RemoteImage = $product->Image = substr($matches[0], 0, strlen($matches[0])-1);
            }
        }
                
        
        $id = $url;
        if(preg_match($config->SkuRegex, $url,$matches)){
            $id= $matches[1];
        }

        if(!is_numeric($id)){
            if(preg_match('/<input.*?id="productMercantId".*?\/>/', $body,$matches)){
                if(preg_match('/value="\d*?"/', $matches[0],$matches)){
                    $matches    = explode("=", $matches[0]);
                    $matches    = str_replace('"', "", $matches[1]);
                    $id         = $matches;
                }
            }
        }
        
        $html = file_get_contents('http://busystock.i.yihaodian.com/restful/detail?mcsite=1&provinceId=20&pmId='. $id);
        $prires = json_decode($html); 
        if ($prires) {
            if(false && $prires->currentTime > time()){
                //这个不会成立
                $product->Price = $prires->promPrice;
            }else{
                // $product->Price = $prires->yhdPrice;
                $product->Price = $prires->currentPrice;
            }
        }
        if(!$product->Price){
            if(preg_match('/价格.*?1号店，品质保证，网上优选/', $body,$matches)){
                $matches    = explode(" ", $matches[0]);
                $matches    = str_replace('￥', "", $matches[1]);
                if(is_numeric($matches)){
                    $product->Price = $matches;
                }
            }
        }
        if(!$product->Price){
            //这里应该无效，应为价格数据不存在页面中
            if(preg_match($config->PriceRegex, $body,$matches)){
                if(count($matches) > 1){
                    $product->Price = $matches[1];
                }
            }
        }
        
        if (!$product->Price) {
            return NULL;
        }
        
        if($product->Price >= 100){
            $product->ShipFee = 0;
        }else{
            $product->ShipFee = 5;
        }
        
        if($config->ShopRegex && preg_match($config->ShopRegex, $body,$matches)){
            $shopName = $matches[1];
        }
        
        
        $product->Sku = $id;
        $product->Url = $url;
        
        $product->Quantity = 999;
        $shopName = '一号店';
        
        return array(
            'product'   => $product, 
            'shopName'  => $shopName,
            'siteName'  => '一号店',
            'shopUrl'   => $url,
            'url'       => $url,
            'siteUrl'   => $uri['scheme'] . '://' . $uri['host']);
    }
    
}